package com.gxa.controller.investment;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.investmentDto.IntentionAgreementDto;
import com.gxa.entity.investment.IntentionAgreement;
import com.gxa.entity.investment.IntentionRegister;
import com.gxa.service.investment.IntentionAgreementService;
import com.gxa.vo.investmentVo.IntentionAgreementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 意向协议单 前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-17 03:04:36
 */
@Api(tags = "过程管理/意向协定")
@RestController
@RequestMapping("/intention/agreement")
public class IntentionAgreementController {

    @Autowired
    private IntentionAgreementService intentionAgreementService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增")
    @PostMapping("/add")
    public Result add(@RequestBody IntentionAgreement intentionAgreement){
        try {
            intentionAgreementService.add(intentionAgreement);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("add failed");
        }
        return Result.success("add success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("修改状态")
    @PutMapping("/update/state/{id}/{state}")
    public Result update(@PathVariable("id") Integer id, @PathVariable("state")Integer state){
        try {
            intentionAgreementService.updateToState(id,state);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("update failed");
        }
        return Result.success("update success");
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("列表接口(条件查询出问题时使用)")
    @GetMapping("/query/list")
    public Result list(){
        try {
            return Result.success(intentionAgreementService.listAll());
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("list failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("条件查询")
    @PostMapping("/query")
    public Result query(@RequestBody IntentionAgreementDto intentionAgreementDto, Integer pageNum, Integer pageSize){

        try {
            PageInfo result = intentionAgreementService.queryByIntentionAgreementDto(intentionAgreementDto, pageNum, pageSize);

            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("query failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询")
    @PostMapping("/query/{id}")
    public Result queryById(@PathVariable("id") Integer id){

        try {
            IntentionAgreementVo result = intentionAgreementService.queryById02(id);

            return Result.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("query failed");
        }

    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("查询登记列表")
    @PostMapping("/childList")
    public Result childList() {
        try {
           List<IntentionRegister> list = this.intentionAgreementService.queryAll();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("query failed");
        }
    }
}
